﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using com.subb.WebInformation;
using WebEntities.Models.WebInfo;
using System.Configuration;
using System.IO;
using WebsiteAdmin.Extensions;

namespace WebsiteAdmin.Controllers
{
    [Authorize]
    public class ExpertController : Controller
    {
        //
        // GET: /Expert/

        public ActionResult Index(int cataid=0)
        {
            ViewBag.CataID = cataid;
            return View();
        }

        public ActionResult GetAllExperts(StoreRequestParameters parms,int cataid=0)
        {
            WI_ExpertsDAL mdal = new WI_ExpertsDAL();
            int ttlCount;
            List<WI_Experts> dbExpertslist = mdal.GetAllExperts(cataid,parms.Start, parms.Limit, out ttlCount);
            return this.Store(dbExpertslist, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteExperts(int id)
        {
            try
            {
                WI_ExpertsDAL mdal = new WI_ExpertsDAL();
              
                    mdal.RemoveExperts(id);
                
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewExpertsWindow(string cataId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建专家资料",
                Width = 600,
                Height = 500,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateExperts"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            if (!string.IsNullOrWhiteSpace(cataId))
            {
                wnd.Loader.Params.Add(new Parameter("cataId", cataId));
            }
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateExperts(int? cataId)
        {
            ViewData["ExpCataID"] = cataId ?? 0;
            WI_ExpertCataDAL ecdal = new WI_ExpertCataDAL();
            List<WI_ExpertCata> cataList = ecdal.GetAllExpertCata();
            ViewData["ExpCata"] = cataList;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateExperts(Experts obj)
        {
            //新建菜单
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                //上传文件
                string savePath = string.Empty;
                string linkPath = string.Empty;
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("ExpertPic");

                    //上传产品封面图
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath +=uploadVFolder+"Expert/";
                            linkPath +=uploadVFolder+"Expert/";
                            savePath = Server.MapPath(savePath);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            string filename=DateTime.Now.Ticks + extName;
                          //  string thumbpath = savePath + DateTime.Now.Ticks  + extName;
                           // savePath = savePath + DateTime.Now.Ticks+"_o" + extName;
                            savePath += filename;
                            linkPath += filename;
                            fpfb1.SaveAs(savePath);
                            //ImgModify imgModify = new ImgModify();
                            //string mode = imgModify.GetMode(savePath);
                            //imgModify.MakeThumbnail(savePath, thumbpath, 1000, 1000, mode);
                            //if (System.IO.File.Exists(savePath))
                            //{
                            //    System.IO.File.Delete(savePath);
                            //}
                        }
                    }
                }
                WI_ExpertsDAL mdal = new WI_ExpertsDAL();
                WI_Experts addObj = new WI_Experts()
                {
                    Achievement = obj.Achievement,
                    CreateTime = DateTime.Now,
                    ExpertCataID = obj.ExpertCataID,
                    ExpertJob = obj.ExpertJob,
                    ExpertName = obj.ExpertName,
                    ExpertPic = linkPath,
                    ExpertPosition = obj.ExpertPosition,
                    Introduction = obj.Introduction,
                    IsShow = byte.Parse(obj.IsShow.ToString()),
                    OtherMsg = obj.OtherMsg,
                    SkillField = obj.SkillField,
                    SortNum = obj.SortNum,
                    IsDelete=0
                     
                };
                mdal.CreateExperts(addObj);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "数据创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditExpertsWindow(int ExpertsId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑专家资料",
                Width = 600,
                Height = 500,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutEdit,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditExperts"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("ExpertsId", ExpertsId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditExperts(int ExpertsId)
        {
            WI_ExpertsDAL mdal = new WI_ExpertsDAL();
            WI_Experts dbExperts = mdal.GetExperts(ExpertsId);
            Experts obj = Experts.AsExpert(dbExperts);
            return View(obj);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditExperts(Experts obj)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {  
                //上传文件
                string savePath = string.Empty;
                string linkPath = string.Empty;
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("ExpertPic");

                    //上传产品封面图
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath += uploadVFolder + "Expert/";
                            linkPath += uploadVFolder + "Expert/";
                            savePath = Server.MapPath(savePath);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            string filename = DateTime.Now.Ticks + extName;
                            savePath += filename;
                            linkPath += filename;
                            fpfb1.SaveAs(savePath);

                        }
                    }
                }
                WI_ExpertsDAL mdal = new WI_ExpertsDAL();
                obj.ExpertPic = linkPath;
                mdal.UpdateExperts(obj.ExpertID, obj);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "数据更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

    }
}
